package com.gec.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysLetter;
import com.gec.model.entity.SysRepair;
import com.gec.model.vo.SysLetterVo;
import com.gec.model.vo.SysRepairVo;
import com.gec.system.service.SysLetterService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 信件表 前端控制器
 * </p>
 *
 * @author lmd
 * @since 2023-12-08
 */
@Api(tags = "信件管理控制器")
@RestController
@RequestMapping("/admin/system/sysLetter")
public class SysLetterController {

    @Autowired
    private SysLetterService sysLetterService;

    @ApiOperation("查询所有信件")
    @GetMapping("/findAllLetter")
    public Result findAllLetter(){
        List<SysLetter> list = this.sysLetterService.list();

        return Result.ok(list);

    }
    @ApiOperation("查询所有信件(根据业主的id查询)")
    @GetMapping("/findAllLetterById/{id}")
    public Result findAllLetterById(@PathVariable Integer id){
        QueryWrapper<SysLetter> queryWrapper=new QueryWrapper<SysLetter>();
        queryWrapper.eq("owner_id", id);
        List<SysLetter> list = this.sysLetterService.list(queryWrapper);

        return Result.ok(list);

    }
    @ApiOperation("分页查询")
    @GetMapping("/{page}/{limit}")
    public Result selectLetterPageVo(@PathVariable Long page,
                                     @PathVariable Long limit,
                                     SysLetterVo sysLetterVo
    ){
        IPage<SysLetter> iPage =new Page<>(page,limit);

        System.out.println("iPage条件："+iPage);

        iPage=this.sysLetterService.selectPage(iPage,sysLetterVo);

        System.out.println("控制层打印楼栋信息："+iPage);
        return Result.ok(iPage);
    }



    //    新增
    @ApiOperation("新增信件信息")
    @PostMapping("/addLetter")
    public Result addLetter(@RequestBody SysLetter sysLetter) {
        boolean save = this.sysLetterService.save(sysLetter);
        return save ? Result.ok() : Result.fail();

    }

    // 修改
    //1.根据id 去得到当前分类
    @ApiOperation("根据id获取维修信息")
    @GetMapping("/findLetterById/{id}")
    public Result findLetterById(@PathVariable Long id)
    {
        SysLetter sysLetter = this.sysLetterService.getById(id);
        return Result.ok(sysLetter);
    }

    // 实现修改
    @ApiOperation("修改信件信息")
    @PostMapping("/updateLetter")
    public Result updateLetter(@RequestBody SysLetter sysLetter)
    {
        boolean b = this.sysLetterService.updateById(sysLetter);
        return b ? Result.ok() : Result.fail();

    }

    //    根据单个id 删除数据
    @ApiOperation("根据id删除信件信息")
    @DeleteMapping("/removeLetterById/{id}")
    public Result removeLetterById(@PathVariable Long id){

        boolean b = this.sysLetterService.removeById(id);
        return b ? Result.ok() : Result.fail();

    }

    // 批量删除
    @ApiOperation("批量删除信件信息")
    @DeleteMapping("/removeLetterByIds")
    public Result removeLetterByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.sysLetterService.removeByIds(ids);

        return b ? Result.ok() : Result.fail();

    }


    // 更改来源
    @ApiOperation("更改信件来源")
    @GetMapping("/updateLetterOrigin/{id}/{origin}")
    public Result updateLetterOrigin(@PathVariable Long id,
                                     @PathVariable Integer origin)
    {
        this.sysLetterService.updateLetterOrigin(id,origin);
        return Result.ok();
    }
}

